<%@ page contentType="text/html; charset=UTF-8" language="java"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>取票机管理 </title>
    		<%@ include file="/WEB-INF/jspf/taglibs.jspf"%>
		<%@ include file="/WEB-INF/jspf/statics.jspf"%>
		<link rel="stylesheet" type="text/css" href="<%=basePath %>/css/jquery.multiselect.css" />
</head>
<%-- 		<%@ include file="/WEB-INF/jspf/taglibs.jspf"%>
		<link rel="stylesheet" type="text/css" href="<%=basePath %>/css/core.css">
 <link rel="stylesheet" type="text/css" href="<%=basePath %>/css/style.css"> 
 <link rel="stylesheet" type="text/css" href="<%=basePath %>/css/jquery-ui.css">
 <link type="text/css" rel="stylesheet" href="<%=basePath %>/css/showBo.css" />
		<link rel="stylesheet" type="text/css" href="<%=basePath %>/css/jquery.multiselect.css" />
</head> --%>
<body>
<div id="wrapper">

    <div class="submit-form pusht">
        <form name="submit-form" id="submit-form">
            <table class="form label-colored-form">
                <c:if test="${operationType=='add'}">
                    <tbody>
                    <tr class="sprite">
                        <td class="label-td"><label>ip地址：</label></td>
                        <td><input type="text" name="ip" id="ip" value="" maxlength="20" style="width:200px;"><span style="color:red;"></span></td>
                    </tr>
                    <tr class="sprite">
                        <td class="label-td"><label>mac地址：</label></td>
                        <td><input type="text" name="macIp" id="macIp" value="" maxlength="20" style="width:200px;"><span style="color:red;"></span></td>
                    </tr>
                     <tr class="sprite">
                        <td class="label-td"><label>所属场馆：</label></td>
                        <td>
                            <select name="venueId" id="venueId">
                            	<c:forEach items="${venueList }" var="venue">
                                	<option value=${venue.id }>${venue.venueName }</option>
                                </c:forEach>
                            </select>
                        </td>
                    </tr>
                    <tr class="sprite">
                        <td class="label-td"><label>所在位置：</label></td>
                        <td><textarea name="position" id="position" style="width:382px;height:100px;resize:none; "></textarea><span style="color:red;"></span></td>
                    </tr>
                   <tr class="sprite">
                        <td class="label-td"><label>所用广告：</label></td>
                        <td>
                            <select name="advertiseList1" id="advertiseList"  multiple="multiple">
                            	<c:forEach items="${advertiseList }" var="advertise">
                                	<option value=${advertise.id}>${advertise.title }</option>
                                </c:forEach>
                            </select>
                        </td>
                    </tr>
                    <tr class="sprite">
                        <td class="label-td"><label>状态：</label></td>
                        <td>
                            <select name="status" id="status">
                                <option value=1>启用</option>
                                <option value=2>禁用</option>
                            </select>
                        </td>
                    </tr>
                    <tr class="sprite">
                        <td class="label-td"><label></label></td>
                        <td>
                            <button class="btn btn-primary" id="btn_confirm">添加</button>
                        </td>
                    </tr>
                    </tbody>
                </c:if>
                
                
                <c:if test="${operationType !='add'}">
                    <input type="hidden" name="id" value="${machine.id}"/>
                    <input type="hidden" name="oldip" value="${machine.ip}"/>
                    <input type="hidden" name="oldmac" value="${machine.macIp}"/>
                    <tbody>
                     <tr class="sprite">
                        <td class="label-td"><label>ip地址：</label></td>
                        <td><input type="text" name="ip" id="ip" value="${machine.ip }" maxlength="20" style="width:200px;"><span style="color:red;"></span></td>
                    </tr>
                    <tr class="sprite">
                        <td class="label-td"><label>mac地址：</label></td>
                        <td><input type="text" name="macIp" id="macIp" value="${machine.macIp}" maxlength="20" style="width:200px;"><span style="color:red;"></span></td>
                    </tr>
                     <tr class="sprite">
                        <td class="label-td"><label>所属场馆：</label></td>
                        <td>
                            <select name="invoiceId" id="invoiceId">
                            	<c:forEach items="${venueList }" var="venue">
                                	<option value="${venue.id }" <c:if test="${machine.venueId==venue.id }">selected</c:if>>${venue.venueName }</option>
                                </c:forEach>
                            </select>
                        </td>
                    </tr>
                    <tr class="sprite">
                        <td class="label-td"><label>所在位置：</label></td>
                        <td><textarea name="position" id="position" style="width:380px;height:100px;resize:none; ">${machine.position }</textarea><span style="color:red;"></span></td>
                    </tr>
                   <tr class="sprite">
                        <td class="label-td"><label>所用广告：</label></td>
                        <td>
                            <select name="advertiseList1" id="advertiseList"  multiple="multiple">
                            	<c:forEach items="${advertiseList }" var="advertise">
                                	<option value="${advertise.id }" <c:forEach items="${machine.advertiseList }" var="machineadvertise"> <c:if test="${machineadvertise.id==advertise.id }">selected</c:if> </c:forEach>>${advertise.title }</option>
                                </c:forEach>
                            </select>
                            <input type="hidden" id="hfexample" />
                        </td>
                    </tr>
                    <tr class="sprite">
                        <td class="label-td"><label>状态：</label></td>
                        <td>
                            <select name="status" id="status">
                                <option value=1 <c:if test="${machine.status==1}">selected</c:if>>启用</option>
                                <option value=2 <c:if test="${machine.status==2}">selected</c:if>>禁用</option>
                            </select>
                        </td>
                    </tr>
                   
                    <tr class="sprite">
                        <td class="label-td"><label></label></td>
                        <td>
                            <button class="btn btn-primary" id="btn_confirm1">保存</button>

                        </td>
                    </tr>
                    </tbody>
                </c:if>
            </table>
             <input type="hidden" value="" name="adveritseStr"/>
        </form>
    </div>
</div>
</body>
</html>
<script type="text/javascript" src="${ctx}/js/jquery.1.7.2.min.js"></script>
<script type="text/javascript" src="<%=basePath%>/js/ui/jquery-ui.min.js"></script>
<script type="text/javascript" src="<%=basePath%>/js/jquery.multiselect.js"></script>

<script type="text/javascript">
  $(document).ready(function () {
      // 给确认按钮绑定点击事件
      $("#btn_confirm").click(function (e) {
          e.preventDefault();
          if(subimtBtn('add')){
          	dialog_close();
          }
      });

      $("#btn_confirm1").click(function (e) {
          e.preventDefault();
          if(subimtBtn('update')){
          	dialog_close();
          }
      });
      $("#ip").focus(function(){
    	  $("#ip").next().text('');
      });
      $("#macIp").focus(function(){
    	  $("#macIp").next().text('');
      });
      $("#position").focus(function(){
    	  $("#position").next().text('');
      });
  }); 
  
  $(document).ready( function() {
		$("#advertiseList").multiselect();
  });
  
  function checkIP(value){
	  var flag = false;
	    var exp=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
	    var reg = value.match(exp);
	    if(reg==null)
	    {
	    	$("#ip").next().text("ip地址错误");
	    	return flag;
	    }
	    $.ajaxSetup({
            async : false
        });
        $.post("${ctx }/equipment/check_invoice_machine_ip", {ip: $("#ip").val()},function (data) {
            if (data == "true") {
            	$("#ip").next().text("该ip地址已经存在");
            } else if (data == "false") {
                flag = true;
            }
        });
	    return flag;
	}
  
  function checkMac(value){
	  var exp = /[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}/;
	    if(!exp.test(value))
	    {
	    	$("#macIp").next().text("macIp地址错误");
	    	return false;
	    }
	    return true;
  }
  
  
  function checkForm(){
	  values = $("#submit-form").serializeArray();  
	  var values, index;  
	  var result=true;
	     for (index = 0; index < values.length; ++index)  
	    {  
	    	 if (values[index].name == "ip")  
		      {  
		    	  if(!checkIP(values[index].value)){
		    		  result=false;
		    		  break;
		    	  }    	  
		      }  
		      if (values[index].name == "macIp")  
		      {  
		    	  if(!checkMac(values[index].value)){
		    		  result=false;
		    		  break;
		    	  }   
		      }
	      if (values[index].name == "position")  
	      {  
	    	  if($("#position").val()=="" || $("#position").val()==null){
	    		  $("#position").next().text("地理位置不能为空宫！");
	    		  result=false;
	    		  break;
	    	  }
	      }
	    }  
	     return result;
  }
  function subimtBtn(type) {  
	  if(!checkForm()){
		  return false;
	  };
      var form = $("#submit-form");
      var url="";
      if(type=="update"){
		  url="${ctx }/equipment/update_invoice_machine"; 
      }else{
		  url="${ctx }/equipment/add_invoice_machine";    	  
      }
      
      var array_of_checked_values = $("#advertiseList").multiselect("getChecked").map(function(){
          return this.value;   
	  }).get();
      $("input[name='adveritseStr']").val(array_of_checked_values);
      
      $.post(url,form.serializeArray(),function(json){    
        	if (json == "success") {
            	parent.document.getElementById('success').click();
            } else {
            	parent.document.getElementById('fail').click();
            }
      });
      
      dialog_close({callBack: function () {
          parent.document.getElementById('btn-primary').click();
      }});
  }  
</script>